﻿using System;
using System.Linq;
using System.Collections.Generic;
using DevExpress.XtraBars;
using BCL.BaseLib;
using BCL.BaseLib.Basics;
using BCL.Component;
namespace BCL.Main
{
    public partial class Form1 : TabForm,  IMainForm
    {
        private IBList ibList;

        private List<FunctionEntity> openfe;
        public Form1()
        {
            InitializeComponent();
            ibList = this.ibList1;
            BCLApplication.MainForm = this;
        }
        public Form1(List<FunctionEntity> fes)
        {
            InitializeComponent();
            ibList = this.ibList1;
            BCLApplication.MainForm = this;
            InitializeComponent();
            openfe = new List<FunctionEntity>();

            this.Functiones = fes;

            LoadPage();
        }


        /// <summary>
        /// 加载需要随程序启动的项与主页
        /// </summary>
        private void LoadPage()
        {
            if (BCLApplication.AppFunMunes == null)
                return;

            foreach (FunctionEntity item in BCLApplication.AppFunMunes)
            {
                if (item.ItemType == FunctionItemType.Item)
                {
                    if (item.IsCreate)
                        AddBarButtonItem(item);
                    if (item.StartLoad || item.Index)
                        App_Click(item);
                }

            }

            


            //加载首页
            App_Click(BCLApplication.AppFunMunes.FirstOrDefault(o => o.Index));
        }
        private void AddBarButtonItem(FunctionEntity fe)
        {
            BarButtonItem barButton = new BarButtonItem();
            barButton.Caption="";
            
            //barButton.Size = new System.Drawing.Size(24,24);
            barButton.ImageOptions.Image = fe.Image?.GetThumbnailImage(18,18,null,System.IntPtr.Zero);
            //barButton.ImageOptions.LargeImage = fe.Image?.GetThumbnailImage(20, 20, null, System.IntPtr.Zero);
            barButton.ItemClick += (send,e)=>App_Click(fe);
            
            tabFormControl1.Items.Add(barButton);
            tabFormControl1.TabRightItemLinks.Add(barButton);
        }


        static int OpenFormCount = 1;

        public List<FunctionEntity> Functiones
        {
            get { return BCLApplication.AppFunTree; }
            set
            {
                if (value != null)
                    RegisteFunctions(value);
                else
                {
                    BCLApplication.AppFunMunes = null;
                    BCLApplication.AppFunTree = null;
                }
                if (ibList != null)
                    ibList.Init();
                //openfe = new List<FunctionEntity>();
                ////tabFormControl1.SelectedPageChanged += (sender, e) => { ((TabFormPage)sender).Tag };
                ////this.tcMainTable.TabControlChanged += (fe, adds) => { if (adds > 0) openfe.Add(fe); else openfe.Remove(fe); };
                //this.Functiones = value;

                LoadPage();
            }
        }
        /// <summary>
        /// 将用户发过来的菜单项注册到全局树形与离散的菜单
        /// </summary>
        /// <param name="feList"></param>
        private void RegisteFunctions(List<FunctionEntity> feList)
        {
            List<FunctionEntity> appFunMunes = new List<FunctionEntity>();
            if (feList != null)
            {
                foreach (var item in feList)
                    appFunMunes.AddRange(FunctionToList(item));
            }
            BCLApplication.AppFunMunes = appFunMunes;
            BCLApplication.AppFunTree = feList;
        }



        /// <summary>
        /// 将树形菜单转换成离散菜单
        /// </summary>
        /// <param name="fe"></param>
        /// <returns></returns>
        private List<FunctionEntity> FunctionToList(FunctionEntity fe)
        {
            List<FunctionEntity> list = new List<FunctionEntity>();
            list.Add(fe);
            if (fe.Items != null)
                foreach (var item in fe.Items)
                    list.AddRange(FunctionToList(item));
            return list;

        }

        public List<FunctionEntity> OpenedFe
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        public Action RefreshAppList
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        public BaseFunctionControl TopControl { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public BaseFunctionControl FootControl { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            ibList.Visible = !ibList.Visible;
        }

        public void OpenFunction(FunctionEntity info)
        {
            App_Click(info);
        }

        public void OpenFunction(BaseFunctionControl control)
        {
            throw new NotImplementedException();
        }

        public void CloseFunction(FunctionEntity info)
        {
            throw new NotImplementedException();
        }

        public void App_Click(FunctionEntity fe)
        {
            if (fe != null)
            {
                // 隐藏菜单面板
                if (ibList != null)
                    ibList.Visible = false;

                if (fe.FunctionType == FunctionType.Function && fe.Custom != null)
                    fe.Custom.FunctionInvoke(this, fe);
                if (fe.FunctionType == FunctionType.Control && fe.Control != null)
                {
                    #region 控件
                    if ((fe.Control.Setting & FunctionControlSetting.UserControl) != 0)
                    {
                        TabFormPage tp = tabFormControl1.Pages.FirstOrDefault(o=>o.Text==fe.Title);
                        if (tp != null)
                        {
                            tabFormControl1.SelectedPage = tp;
                            return;
                        }
                        tp = new TabFormPage();
                        tp.Text = fe.Title;
                        TabFormContentContainer tc = new TabFormContentContainer();
                        tc.Dock = System.Windows.Forms.DockStyle.Fill;
                        fe.Control.Dock = System.Windows.Forms.DockStyle.Fill;
                        tc.Controls.Add(fe.Control);
                        tp.ContentContainer = tc;
                        tp.Tag = fe;
                        Controls.Add(tc);
                        tabFormControl1.Pages.Add(tp);
                        tabFormControl1.SelectedPage = tp;
                    }
                    if ((fe.Control.Setting & FunctionControlSetting.ShowDialog) != 0 || (fe.Control.Setting & FunctionControlSetting.Show) != 0)
                    {
                        BaseShowControl sc = fe.Control as BaseShowControl;
                        if (sc != null)
                            if ((fe.Control.Setting & FunctionControlSetting.ShowDialog) != 0)
                                sc.ShowDialog(this);
                            else
                                sc.Show();
                    }
                    #endregion
                }

            }
        }

        public void RefreshMessageStates()
        {
            throw new NotImplementedException();
        }

        public void OpenMenu()
        {
            throw new NotImplementedException();
        }

        public void Msg(FunctionEntity fe)
        {
            throw new NotImplementedException();
        }

    }
}
